package com.aiju.cash.ui.enter.util;

import android.text.TextUtils;

import com.aiju.app.DataProviderManager;
import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseActivity;
import com.aiju.cash.core.api.EnterApi;
import com.aiju.forexpand.addresspicker.AddressPicker;
import com.aiju.forexpand.addresspicker.model.CityModel;
import com.aiju.forexpand.addresspicker.model.DistrictModel;
import com.aiju.forexpand.addresspicker.model.ProvinceModel;
import com.aiju.utils.L;
import com.aiju.utils.PreferencesUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-06-05 09:22
 * @version: V1.0
 * @Description: 城市 品类数据
 */
public class CityData {

    public static final String CITY = "cityData";
    public static final String CATEGORY = "categoryData";

    private static List<ProvinceModel> provinceList = new ArrayList<>();

    public static List<ProvinceModel> getCityData() {
        return provinceList;
    }

    //获得全部地址
    public static String getAllAddress(String province, String city, String area) {
        return getAddress(province) + "-" + getAddress(city) + "-" + getAddress(area);
    }

    //获得全部地址
    public static String getAllCode(String province, String city, String area) {
        return getCode(province) + getCode(city) + getCode(area);
    }

    //获得地址
    public static String getAddress(String address) {
        return address.split("_")[0];
    }

    //获得地址编号
    public static String getCode(String address) {
        return address.split("_")[1];
    }

    /**
     * 获取地区接口
     *
     * @param activity
     * @param changeCity
     */
    public static void showAreaDialog(final BaseActivity activity, final AddressPicker.OnChangeCity changeCity) {
        provinceList.clear();
        if (!isCache(CITY, activity, changeCity)) {
            new EnterApi(activity.getDialogHelper()).getArea(new SimpleHttpCallback() {
                @Override
                protected void onDataSuccess(String data) {
                    parserCity(data);
                    creatDialog(CITY, activity, changeCity);
                    //请求后缓存
                    PreferencesUtils.putString(DataProviderManager.getContext(), CITY, data);
                }
            });
        }

    }

    /**
     * 获取分类接口
     *
     * @param activity
     * @param changeCity
     */
    public static void showCategoryDialog(final BaseActivity activity, final AddressPicker.OnChangeCity changeCity) {
        provinceList.clear();
        if (!isCache(CATEGORY, activity, changeCity)) {
            new EnterApi(activity.getDialogHelper()).getCategory(new SimpleHttpCallback() {
                @Override
                protected void onDataSuccess(String data) {
                    parserCity(data);
                    creatDialog(CATEGORY, activity, changeCity);
                    //请求后缓存
                    PreferencesUtils.putString(DataProviderManager.getContext(), CATEGORY, data);
                }
            });
        }
    }

    /**
     * 是否有缓存，有显示
     *
     * @return
     */
    private static boolean isCache(String key, BaseActivity activity, AddressPicker.OnChangeCity changeCity) {
        final String city = PreferencesUtils.getString(DataProviderManager.getContext(), key);
        if (!TextUtils.isEmpty(city)) {
            parserCity(city);
            creatDialog(key, activity, changeCity);
            return true;
        }
        return false;
    }

    /**
     * 创建对话框
     *
     * @param activity
     * @param changeCity
     */
    private static void creatDialog(String key, BaseActivity activity, AddressPicker.OnChangeCity changeCity) {
        AliAddress addressPicker = new AliAddress();
        addressPicker.selectAddressDialog(activity, null);
        addressPicker.setChangeCity(changeCity);
        if (key.equals(CATEGORY)) {
            addressPicker.getTitleTextView().setText("选择经营品类");
        }
    }

    /**
     * 解析城市数据
     *
     * @param data
     */
    private static void parserCity(String data) {
        try {
            JSONObject json = new JSONObject(data);
            Iterator<String> keys = json.keys();
            while (keys.hasNext()) {
                ProvinceModel provinceModel = new ProvinceModel();
                String next = keys.next();
                provinceModel.setName(next);

                JSONArray jsonArray = json.getJSONArray(next);
                List<CityModel> cityList = new ArrayList<>();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Iterator<String> keys1 = jsonObject.keys();

                    CityModel cityModel = new CityModel();
                    while (keys1.hasNext()) {
                        String next1 = keys1.next();
                        JSONArray jsonArray1 = jsonObject.getJSONArray(next1);
                        cityModel.setName(next1);
                        List<DistrictModel> districtModels = new ArrayList<>();
                        for (int j = 0; j < jsonArray1.length(); j++) {
                            DistrictModel districtModel = new DistrictModel();
                            districtModel.setName(jsonArray1.get(j).toString());
                            districtModels.add(districtModel);
                        }
                        cityModel.setDistrictList(districtModels);
                        cityList.add(cityModel);
                    }
                    provinceModel.setCityList(cityList);
                }
                provinceList.add(provinceModel);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        L.d(provinceList.size());
    }
}
